/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.service4mysql;

import com.zjdt.epserver.db.dao.UserDao;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dusin
 */
public class UserServiceImpl {
    static Logger logger = Logger.getLogger(UserServiceImpl.class);
    UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }    
    
    public Map<String,Object> login(String username,String password){
        return userDao.login(username, password);
    }
     public boolean create(String username,String name,String phoneMobile,String note){
        return userDao.doCreate(new Object[]{username,"888888",name,phoneMobile,note}); //默认密码888888
    }
     
    public List<Map<String,Object>> findAll(){
        List<Map<String,Object>> users =  userDao.findAll();
        for(Map<String,Object> user:users){
            String username = (String)user.get("username");
            List<Map<String,Object>> modules = userDao.findAllModuleByUsername(username);
            StringBuilder modulesNames=new StringBuilder();   //使用逗号分隔的模块名称
            if(modules!=null){
                for(Map<String,Object> m:modules){
                    modulesNames.append((String)m.get("name")).append(",");                
                }
                if(modulesNames.length()>0&&modulesNames.charAt(modulesNames.length()-1)==','){
                    modulesNames.deleteCharAt(modulesNames.length()-1);
                }
                user.put("modules", modulesNames.toString());
            }else{
                user.put("modules", "");
            }
        }
        return users;
    }
     public boolean update(int id,String username,String name,String phoneMobile,String note){
        return userDao.doUpdateById(new Object[]{username,name,phoneMobile,note,id});
    }
     
      public boolean remove(int id){
        return userDao.doDeleteById(id);
    }
    public boolean updatePassword(int id,String password){
        return userDao.doUpdatePassword(id, password);
    }
     public boolean changePasswordByUsername(String username,String newPassword){
        return userDao.doUpdatePasswordByUsername(username, newPassword);
    }
    public boolean updatePhone(int id,String phoneNumber){
        return userDao.doUpdateMobile(id, phoneNumber);
    }
    
    public boolean trUpdateModules(String username,String[] menuCodes){
        userDao.doDeleteAllModuleByUsername(username);
        return userDao.doCreateAllModuleByUsername(username, menuCodes);
    }
    
    public List<Map<String,Object>> findAllModules(String username){
        return userDao.findAllModuleByUsername(username);
    }
}
